package edu.ccut.saturn.studio.wfg.wfgmodel;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.search.IObserver;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher.SearchResult;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.freemaker.FreemarkerUtils;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;
import edu.ccut.saturn.studio.export.createhtml.Parameters;
import edu.ccut.saturn.studio.wfg.dialog.subactivitydialog.SaturnSubActivityDialog;
import edu.ccut.saturn.studio.wfg.figure.SaturnFigure;
import edu.ccut.saturn.studio.wfg.model.Shape;

public class SaturnSubActivityNode extends Shape {

	private static final long serialVersionUID = 1L;
	
	private static int number = 1;
	
	private Image subActivityPictureSmall = Activator.getImage(Images.SMALL_SUB_ACTIVITY);
	
	private String polymeMode = "1";
	
	private String transferMode = "1";
	
	private String subProcess = "";
	
	private List<SaturnAttribute> formDataAttributeList = new ArrayList<SaturnAttribute>();

	public Image getSmallIcon() {
		return subActivityPictureSmall;
	}

	public void setSmallIcon(Image subActivityPictureSmall) {
		this.subActivityPictureSmall = subActivityPictureSmall;
	}

	public String getPolymeMode() {
		return polymeMode;
	}

	public void setPolymeMode(String polymeMode) {
		this.polymeMode = polymeMode;
	}
    
	
	public String getTransferMode() {
		return transferMode;
	}

	public void setTransferMode(String transferMode) {
		this.transferMode = transferMode;
	}

	public String getSubProcess() {
		return subProcess;
	}

	public void setSubProcess(String subProcess) {
		this.subProcess = subProcess;
	}

	public List<SaturnAttribute> getFormDataAttributeList() {
		return formDataAttributeList;
	}

	public void setFormDataAttributeList(List<SaturnAttribute> formDataAttributeList) {
		this.formDataAttributeList = formDataAttributeList;
	}
	
	public SaturnSubActivityNode(){
		super.setName(SaturnName.KEY_NODE_NAME_ZILIUCHENG + number++);
		super.setSize(new Dimension(50, 50));
	}
	
	@Override
	public Image getIcon() {
		return Activator.getImage(Images.BIG_SUB_ACTIVITY);
	}
	
	@Override
	public IFigure getFigure() {
		SaturnFigure saturnFigure = new SaturnFigure();
		saturnFigure.setIcon(Activator.getImage(Images.BIG_SUB_ACTIVITY));
		saturnFigure.setName(super.getName());
		return saturnFigure;
	}
	
	@Override
	public Node doRefresh(Document document) {
		Node subActivityNode = document.createElement(SaturnName.XML_NAME);
		Node rootNode = document.getDocumentElement();
		SaturnW3CUtil.createChildNode(subActivityNode, SaturnName.KEY_NODE_ELEMENT_POLYMEMODE, this.polymeMode);
		SaturnW3CUtil.createChildNode(subActivityNode, SaturnName.KEY_NODE_ELEMENT_TRANSFERMODE, this.transferMode);
		SaturnW3CUtil.createChildNode(subActivityNode, SaturnName.KEY_NODE_ELEMENT_SUBPROCESS, this.subProcess);
		super.createNodes(document, subActivityNode, this.getFormDataAttributeList(),SaturnName.KEY_NODE_ELEMENT_FORMDATA,SaturnName.KEY_NODE_NAME_DATA);
		SaturnW3CUtil.createAttribute(subActivityNode, SaturnName.XML_TYPE, SaturnName.KEY_NODE_ATTRIBUTE_SUB);
		rootNode.appendChild(subActivityNode);
		return subActivityNode;
	}
	
	@Override
	public Dialog getDialog() {
		SaturnSubActivityDialog dialog = new SaturnSubActivityDialog(Display
				.getDefault().getShells()[0], this);
		return dialog;
	}
	
	@Override
	protected Node createChildNode(Document document, SaturnAttribute attrib, String name) {
		return attrib.creatNode(document, name);
	}
	
//	public Map<String, Document> keyMapInWorkspace() {
//		return ImplMap.mapfullkeyInworkspace("wf", "information", "key");
//	}

//	public Map<String, Document> keyMapInProject() {
//		return ImplMap.mapfullkeyInProject(super.findTheProject(), "wf",
//				"information", "key");
//	}

	// 搜索工作流待定
	
	@Override
	public Shape cloneShape(Shape shape) {
		SaturnSubActivityNode saturnSubActivityNode = (SaturnSubActivityNode)shape;
		SaturnSubActivityNode newSaturnSubActivityNode = new SaturnSubActivityNode();
		newSaturnSubActivityNode.setName(saturnSubActivityNode.getName());
		newSaturnSubActivityNode.setId(saturnSubActivityNode.getId());
		newSaturnSubActivityNode.setNextId(saturnSubActivityNode.getNextId());
		newSaturnSubActivityNode.setDescription(saturnSubActivityNode.getDescription());
		newSaturnSubActivityNode.setPolymeMode(saturnSubActivityNode.getPolymeMode());
		newSaturnSubActivityNode.setTransferMode(saturnSubActivityNode.getTransferMode());
		newSaturnSubActivityNode.setSubProcess(saturnSubActivityNode.getSubProcess());
		newSaturnSubActivityNode.setSmallIcon(saturnSubActivityNode.getSmallIcon());
		return newSaturnSubActivityNode;
	}
	public List<SearchResult> keyMapInWorkspace(IObserver observer) {
		
		List<SearchResult> list = null;
		String[] suffixes = {"wf"};
		
		try {
			list = SaturnFileSearcher.getValueAndDocumentMapInWorkspace(suffixes , observer, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	
	public List<SearchResult> keyMapInProject(IObserver observer) {
		List<SearchResult> list = null;
		
		try {
			list = SaturnFileSearcher.getValueAndDocumentMapInProject(super.findTheProject(), "wf",
					observer, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public Parameters createNodeHtml(String filePath) {
		
		Map<String, String> infos = new HashMap<String, String>();
		String name = getName();
		Parameters para = new Parameters();

		infos.put("name", getName());
		infos.put("id", getId());
		infos.put("polymtype", transfer(getPolymeMode()));
		infos.put("description", getDescription());

		para.setInfourl(name + ".html");
		para.setInfoname(name);
		para.setDescription(getDescription());
		para.setNodeType(Messages.getString("Wfg.SubFlowNode")); //$NON-NLS-1$
		para.setImagename("sub_activity");
		String templatePath = "template/export/wfg/sub";
		String Path = filePath + File.separator + name + ".html";
		FreemarkerUtils.writeTemplateToFile(templatePath, infos, Path);
		
		return para;
	}
	
	/**
	 * 对聚合模式进行转换
	 * 
	 * @param startPolicy
	 * @return Parameters
	 */
	private static String transfer(String polymeMode) {
		if (polymeMode.equals("1")) {
			return Messages.getString("Wfg.OnePoly"); //$NON-NLS-1$
		} else if (polymeMode.endsWith("2")) {
			return Messages.getString("Wfg.AllPoly"); //$NON-NLS-1$
		} else {
			return Messages.getString("Wfg.ManyPoly"); //$NON-NLS-1$
		}
	}
}
